package com.example.user.myhjjf_app.view;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.example.user.myhjjf_app.HJJF_Url;
import com.example.user.myhjjf_app.R;
import com.example.user.myhjjf_app.model.Model_Banka;
import com.example.user.myhjjf_app.model.Model_Content;
import com.example.user.myhjjf_app.model.Model_Regional;
import com.example.user.myhjjf_app.presenter.Implementation_operation;
import com.example.user.myhjjf_app.presenter.Presenter_Ptn_adapter;
import com.example.user.myhjjf_app.presenter.SiteOperation;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.adapter.abslistview.MultiItemTypeAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Activity_buuAdd extends AppCompatActivity {

    @BindView(R.id.btn_back)
    LinearLayout btnBack;
    @BindView(R.id.tx_title)
    TextView txTitle;
    @BindView(R.id.btn_edit)
    LinearLayout btnEdit;
    @BindView(R.id.add_mc)
    EditText addMc;
    @BindView(R.id.add_bm)
    EditText addBm;
    @BindView(R.id.add_wl)
    EditText addWl;
    @BindView(R.id.sdf)
    ListView sdf;
    @BindView(R.id.bt_add_qr)
    Button btAddQr;
    @BindView(R.id.bt_add_bk)
    Button btAddBk;

    SiteOperation siteOperation;
    String type;
    Model_Content.ValueBean bean;
    Model_Banka banka ;
    MultiItemTypeAdapter adapter;
    List<Model_Banka.ValueBean> value =null;
    private EditText inputServer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buu_add);
        ButterKnife.bind(this);
        Bundle bundle = getIntent().getExtras();
        inputServer = new EditText(this);
        siteOperation = new Implementation_operation(this);
        type = bundle.getString("type");
        sdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("value",value.get(i));
                bundle.putString("id",bean.getId());
                ActivityUtils.startActivity(bundle,Activity_bklist.class);
            }
        });
        if (!type.equals("add")){
            bean = (Model_Content.ValueBean) bundle.getSerializable("bean");

            addMc.setText(bean.getName());
            addBm.setText(bean.getCode());
            addWl.setText(bean.getNet_type());
            if (type.equals("select")) {
                txTitle.setText("BBU详情");
                addMc.setEnabled(false);
                addBm.setEnabled(false);
                addWl.setEnabled(false);
                btAddQr.setVisibility(View.GONE);
                init();
            }
        }else {
            txTitle.setText("添加BBU");
            btAddBk.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.btn_back, R.id.btn_edit, R.id.bt_add_qr, R.id.bt_add_bk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_edit:
                showMenu(btnEdit);
                break;
            case R.id.bt_add_qr:
                siteOperation = new Implementation_operation(Activity_buuAdd.this);
                HashMap map = new HashMap();
                String url = HJJF_Url.getHJJF_Url().login("api/data/bbu_add.do");
                if (type.equals("select")) {
                    url = HJJF_Url.getHJJF_Url().login("api/data/bbu_edit.do");
                    map.put("id", bean.getId());
                }
                map.put("name", addMc.getText().toString());
                map.put("code", addBm.getText().toString());
                map.put("netType", addWl.getText().toString());
                map.put("zoneId", Model_Regional.getRegional().getZoneId());
                map.put("siteId", Model_Regional.getRegional().getSiteId());
                map.put("departId", Model_Regional.getRegional().getDepartId());
                Iterator iterator = map.entrySet().iterator();
                int i = 0;
                while (iterator.hasNext()){
                    Map.Entry entry = (Map.Entry) iterator.next();
                    if (entry.getValue().equals("")){
                        Toast.makeText(this, "所有选项不能为空~~", Toast.LENGTH_SHORT).show();
                        i=0;
                        break;
                    }else {
                        i=1;
                    }
                }
                if (i==1){
                    siteOperation.Add(map,url);
                }
                break;
            case R.id.bt_add_bk:
                new AlertDialog.Builder(this).setTitle("请输入板卡名称")
                        .setView(inputServer)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = inputServer.getText().toString();
                                OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/data/banka_add.do"))
                                        .tag(this)
                                        .params("name",name)
                                        .params("pid",bean.getId())
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onSuccess(Response<String> response) {
                                                try {
                                                    JSONObject jsonObject = new JSONObject(response.body());
                                                    String mess = jsonObject.getString("value");
                                                    Toast.makeText(Activity_buuAdd.this, mess, Toast.LENGTH_SHORT).show();
                                                    init();
                                                } catch (JSONException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        });
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
                break;
        }
    }
    private void init(){
        value = new ArrayList<>();
        OkGo.<String>post(HJJF_Url.getHJJF_Url().login("api/site/bk_list.do"))
                .tag(this)
                .params("id",bean.getId())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        banka = JSON.parseObject(response.body(), Model_Banka.class);
                        for (int i = 0; i < banka.getValue().size(); i++) {
                            value.add(banka.getValue().get(i));
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
        adapter = new MultiItemTypeAdapter(this, value);
        adapter.addItemViewDelegate(new Presenter_Ptn_adapter());
        sdf.setAdapter(adapter);
    }


    private void showMenu(View view){
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this,view);
        //menu布局
        popupMenu.getMenuInflater().inflate(R.menu.menu,popupMenu.getMenu());
        //点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.btn_menu_edit:
                        txTitle.setText("PTN3900编辑");
                        addMc.setEnabled(true);
                        addBm.setEnabled(true);
                        addWl.setEnabled(true);
                        btAddBk.setVisibility(View.GONE);
                        btAddQr.setVisibility(View.VISIBLE);
                        break;
                    case R.id.btn_menu_delete:
                        String url = HJJF_Url.getHJJF_Url().login("api/data/bbu_drop.do");
                        siteOperation.delete(url,bean.getId());
                        break;
                }
                return false;
            }
        });
        //关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {

            }
        });
        popupMenu.show();
    }
}
